package com.example.a2306_fx.fg

import androidx.lifecycle.viewModelScope
import com.example.base.entity.CategoryEntity
import com.example.base.entity.ProductEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class CategoryViewModel:BaseViewModel() {
    //分类列表
    val categorySuccess = SingleLiveData<Res<CategoryEntity>>()
    val categoryFail = SingleLiveData<String>()
    fun category(){
        viewModelScope.launch {
            try {
                categorySuccess.postValue(apiService.category())
            } catch (e: Exception) {
                categoryFail.postValue(e.message.toString())
            }
        }
    }
    //商品列表
    val productSuccess = SingleLiveData<Res<ProductEntity>>()
    val productFail = SingleLiveData<String>()
    fun product(cid:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                productSuccess.postValue(apiService.product(cid, limit, page))
            } catch (e: Exception) {
                productFail.postValue(e.message.toString())
            }
        }
    }
}